package com.edu.filter;

import com.edu.pojo.users;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest)req;
        HttpServletResponse servletResponse = (HttpServletResponse) resp;
        HttpSession session = servletRequest.getSession();
        String path = servletRequest.getRequestURI();
        users user = (users)session.getAttribute("user");

        if (path.indexOf("index.jsp") > -1 || path.indexOf("ChairLogin.jsp") > -1
                || path.indexOf("MemberLogin.jsp") > -1 || path.indexOf("MinisterLogin.jsp") > -1
                || path.indexOf("login") > -1 ||  path.indexOf(".jpg") > -1
                || path.indexOf(".html") > -1 || path.indexOf(".png") > -1 || path.indexOf(".css") > -1
                || path.indexOf(".js") > -1  || path.indexOf("ToChairLogin") > -1 || path.indexOf("ToMemberLogin") > -1
                || path.indexOf("ToMinisterLogin") > -1 || path.indexOf("showannouncement")>-1 ||path.indexOf("showNeedFile")>-1 ||path.indexOf("showFile")>-1 || path.indexOf("fileDownload")>-1)
        {
            chain.doFilter(servletRequest, servletResponse);
            return;
        }
        if(user == null || "".equals(user)) {
            servletResponse.sendRedirect("/managementSystem/index.jsp");
        }else {
            chain.doFilter(servletRequest, servletResponse);
        }

    }

    public void destroy() {

    }
}
